﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.Resources;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
using System.Security.Cryptography;

namespace WLS
{
    public partial class Form1 : Form
    {
        CultureInfo cultura;
        ResourceManager manager;

        //formy
        record fr1;
        setting fr2;
        splash sp;



        // kdyz se zavre nejaky form aby vzdy se vyvolal language
        //nastavit na static- vyjimase z objetku

        //Proměné
        ArrayList video;
        all_setting p;
        public string log;

        public Form1()
        {
            this.Visible = false;
            this.Enabled = false;
            sp = new splash();
            sp.Show();

            InitializeComponent();
            Application.DoEvents();
            cultura = CultureInfo.CurrentCulture;
            video = new ArrayList();
        }

        /// <summary>
        /// Změní jazyk
        /// </summary>
        /// <param name="jazyk">Aktuální jazyk např.: cs-CZ,en-US</param>
        void jazyk_JazykZmenen(string jazyk)
        {
            cultura = CultureInfo.CreateSpecificCulture(jazyk);
            manager = new ResourceManager("WLS.Res", typeof(Form1).Assembly);
            button1.Text = manager.GetString("record", cultura);
            button2.Text = manager.GetString("setting", cultura);
            button3.Text = manager.GetString("exit", cultura);
            this.Text = manager.GetString("mainmenu", cultura);
            //log += manager.GetString("zmenenjazyk", cultura);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Load_setting();
            Load_video();
            p = new all_setting(p.sl_ac, p.sl_pass, p.yt_ac, p.yt_pass, p.log, p.lang, p.up_messages,p.encrypt_pass,p.delete_video);
            p.JazykZmenen += new all_setting.ZmenaJazyku(jazyk_JazykZmenen);
            jazyk_JazykZmenen(p.jazyk);
            this.Visible = true;
            this.Enabled = true;
            sp.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (fr1 == null) fr1 = new record(new set(p, video), ref p);
            else fr1.Tag = new set(p, video);
            fr1.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (fr2 == null) fr2 = new setting(p, ref p);
            else fr2.Tag = p;
            fr2.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Save_setting();
            Save_video();
            Application.Exit();
        }

        private void Form1_Closing(object sender, CancelEventArgs e)
        {
            Save_setting();
            Save_video();
        }


        /// <summary>
        /// Uložení nastavení
        /// </summary>
        private void Save_video()
        {
            try
            {
                string cesta = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
                Videos pe = new Videos();
                foreach (data v in video)
                {
                    pe.video.Add(new data(v.nazev, v.uplaod, v.pripona, v.velikost, v.datum, v.path, v.law, v.silver, v.yout));

                }
                FileStream fs = new FileStream(cesta + "\\video.xml", FileMode.Create);
                XmlSerializer xs = new XmlSerializer(typeof(Videos));
                xs.Serialize(fs, pe);
                fs.Close();
            }
            catch { }

        }

        /// <summary>
        /// Uložení videí
        /// </summary>
        private void Save_setting()
        {
            if (p.encrypt_pass == true)
            {
                p.sl_pass=sifruj(p.sl_ac,p.sl_pass);
                p.yt_pass = sifruj(p.yt_ac,p.yt_pass);
            }
            if (p.log.Length >= 1000)
                p.log = p.log.Substring(0, 1000);
            string cesta = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            FileStream fs = new FileStream(cesta + "\\setting.xml", FileMode.Create);
            XmlSerializer xs = new XmlSerializer(typeof(all_setting));
            xs.Serialize(fs, p);
            fs.Close();


        }

        /// <summary>
        /// Načtení videí
        /// </summary>
        private void Load_video()
        {
            try
            {
                string cesta = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
                if (File.Exists(cesta + "\\setting.xml"))
                {
                    FileStream fs = File.OpenRead(cesta + "\\video.xml");
                    XmlSerializer xs = new XmlSerializer(typeof(Videos));
                    Videos pe = (Videos)xs.Deserialize(fs);
                    fs.Close();
                    video = new ArrayList();
                    for (int i = 0; i < pe.video.Count; i++)
                    {
                        if (File.Exists(((data)pe.video[i]).path))
                        {
                            video.Add(new data(((data)pe.video[i]).nazev, ((data)pe.video[i]).uplaod, ((data)pe.video[i]).pripona, ((data)pe.video[i]).velikost, ((data)pe.video[i]).datum, ((data)pe.video[i]).path, ((data)pe.video[i]).law, ((data)pe.video[i]).silver, ((data)pe.video[i]).yout));
                        }
                    }
                }
            }
            catch { }


        }

        /// <summary>
        /// Načtení nastavení
        /// </summary>
        private void Load_setting()
        {
            try
            {
                string cesta = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
                if (File.Exists(cesta + "\\setting.xml"))
                {
                    FileStream fs = File.OpenRead(cesta + "\\setting.xml");
                    XmlSerializer xs = new XmlSerializer(typeof(all_setting));
                    p = (all_setting)xs.Deserialize(fs);
                    fs.Close();
                    cultura = CultureInfo.CreateSpecificCulture(p.lang);
                    p.log = p.log.Replace("\n", "\r\n");
                    if(p.encrypt_pass==true)
                    {

                        p.sl_pass = desifruj(p.sl_ac,p.sl_pass);
                        p.yt_pass = desifruj(p.yt_ac,p.yt_pass);
                    }
                }
                else
                    p = new all_setting("", "", "", "", "", "en-US", true,true,true);
            }
            catch
            {
                p = new all_setting("", "", "", "", "", "en-US", true,true,true);
            }

        }

        private byte[] getklic(int poc,string login)
        {
            if (login == "")
            {
                login = "WLSwlsWLSasdfgqwertWLSwlsWLS";
            }
            byte[] input = Encoding.UTF8.GetBytes(login);
            byte[] vys = new byte[poc];
            int p = poc;
            for (int i = 0; i < vys.Length; i++)
            {
                if (p > input.Length - 1)
                {
                    p = 0;
                }
                vys[i] = input[p];
                p++;
            }
            return vys;
        }

        public string sifruj(string login,string pass)
        {
            RijndaelManaged klic = new RijndaelManaged();
            ICryptoTransform encryptor = klic.CreateEncryptor(getklic(32,login), getklic(16,login));
            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
            byte[] input = Encoding.UTF8.GetBytes(pass);
            csEncrypt.Write(input, 0, input.Length);
            csEncrypt.FlushFinalBlock();
            return (Convert.ToBase64String(msEncrypt.ToArray()));
        }

        public string desifruj(string login,string passs)
        {
            byte[] clearData = Convert.FromBase64String(passs);
            MemoryStream ms = new MemoryStream();
            Rijndael alg = Rijndael.Create();
            alg.Key = getklic(32,login);
            alg.IV = getklic(16,login);
            CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(clearData, 0, clearData.Length);
            cs.Close();
            byte[] encryptedData = ms.ToArray();
            return (Encoding.UTF8.GetString(encryptedData, 0, encryptedData.Length));
        }




    }
}